package com.bsoft.gol.hcb.service.evaluation;

import com.bsoft.gol.hcb.bean.Vo.ConsultDeptEvaluationVo;
import com.bsoft.gol.hcb.bean.request.ConsultDetailRequest;
import com.bsoft.gol.hcb.dao.evaluation.BaseEvaluationDefineDAO;
import com.google.common.collect.Lists;
import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import com.bsoft.gol.hcb.dao.evaluation.BaseEvaluationItemDAO;
import com.bsoft.gol.hcb.dao.evaluation.BaseEvaluationLabelDAO;
import com.bsoft.gol.hcb.dao.evaluation.BaseEvaluationRecordDAO;
import hcn.bean.UserVo;
import hcn.util.ConsultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import pcn.bean.consultation.EvaluationLabelBean;
import pcn.bean.consultation.PatientEvaluateDetailQo;
import pcn.consultation.BaseEvaluationLabel;
import pcn.enums.DictionaryEnum;
import pcn.service.BaseEvaluationRecordService;
import pcn.service.consultation.DoctorEvaluateService;
import pcn.sign.BaseEvaluationDefine;
import pcn.sign.BaseEvaluationItem;
import pcn.sign.BaseEvaluationRecord;
import pcn.vo.consultation.BaseEvaluationLabelrecordVo;
import pcn.vo.consultation.EvaluationRecordVo;
import pcn.vo.consultation.PatientEvaluateDetailVo;
import pcn.vo.sign.BaseEvaluationLabelVo;
import pcn.vo.sign.EvaluationRecordDetailVo;
import service.rpc.BaseUserManageUnitIntf;
import service.rpc.ConsultAskServiceIntf;
import com.bsoft.gol.hcb.utils.CommonUtils;

import java.util.*;
@SsdevService("doctorEvaluateService")
public class DoctorEvaluateServiceImpl implements DoctorEvaluateService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DoctorEvaluateServiceImpl.class);


    @Autowired
    private BaseEvaluationDefineDAO baseEvaluationDefineDAO;
    @Autowired
    private BaseEvaluationItemDAO baseEvaluationItemDAO;
    @Autowired
    private BaseEvaluationLabelDAO baseEvaluationLabelDAO;
    @Autowired
    private BaseEvaluationRecordDAO baseEvaluationRecordDAO;
    @Autowired
    private BaseEvaluationRecordService baseEvaluationRecordService;
    @Autowired
    private BaseUserManageUnitIntf baseUserManageUnitService;
    @Autowired
    private ConsultAskServiceIntf consultAskServiceIntf;

    //获取评价配置
    @RpcService
    public Map<String, Object> findBaseEvaluationDefineAndItem(String businessType) {
        Map<String, Object> map = new HashMap<>();
        BaseEvaluationDefine baseEvaluationDefine = baseEvaluationDefineDAO.getBaseEvaluationDefineByBussinessType(businessType, (byte) 1);
        if (CommonUtils.isNotEmpty(baseEvaluationDefine)) {
            List<BaseEvaluationItem> baseEvaluationItems = baseEvaluationItemDAO.getBaseEvaluationItemByDefineId(baseEvaluationDefine.getId());
            List<BaseEvaluationLabel> baseEvaluationLabels = baseEvaluationLabelDAO.findBaseEvaluationLabelByDefineId(baseEvaluationDefine.getId());
            map.put("define", baseEvaluationDefine);
            map.put("itemList", baseEvaluationItems);
            map.put("labelList", baseEvaluationLabels);
        }
        return map;
    }


    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    @RpcService
    public void deleteLabel(int id) {
        try {
            baseEvaluationRecordService.deleteLabel(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 审核
     *
     * @param recordId
     * @param verifyState
     * @param verifyDesc
     */
    @Override
    @RpcService
    public void reditConsultEvaluation(int recordId, String verifyState, String verifyDesc) throws ControllerException {
        try {
            BaseEvaluationRecord baseEvaluationRecord =baseEvaluationRecordService.queryBaseEvaluationRecord(recordId);
            if (null == baseEvaluationRecord) {
                throw new ControllerException("该评价记录不存在");
            }
            if(!baseEvaluationRecord.getVerifyState().equals("0")){
                throw new ControllerException("审核失败：该评价记录已审核");
            }
            baseEvaluationRecord.setVerifyState(verifyState);
            baseEvaluationRecord.setVerifyDesc(verifyDesc);
            baseEvaluationRecord.setVerifyTime(new Date());
            UserVo userVo = baseUserManageUnitService.getUserIdByOnline();
            baseEvaluationRecord.setVerifyUser(userVo.getUserId());
            baseEvaluationRecordService.reditConsultEvaluation(baseEvaluationRecord);
        } catch (ControllerException e) {
            throw new ControllerException(e.getMessage());
        } catch (Throwable e) {
            e.printStackTrace();
            throw new ControllerException("审核评价失败");
        }
    }

    /**
     * 查询详情
     *
     * @param recordId
     * @return
     */
    @Override
    @RpcService
    public EvaluationRecordVo getConsultEvaluationDetail(int recordId) {
        EvaluationRecordVo evaluationRecordVo = new EvaluationRecordVo();
        try {
            BaseEvaluationRecord baseEvaluationRecord = baseEvaluationRecordService.queryBaseEvaluationRecord(recordId);
            if (CommonUtils.isEmpty(baseEvaluationRecord)) {
                return null;
            }
            UserVo user = baseUserManageUnitService.getUserById(baseEvaluationRecord.getUserId());
            evaluationRecordVo.setId(recordId);
            if (user != null) {
                evaluationRecordVo.setUserName(user.getUserName());
            }else{
                evaluationRecordVo.setUserName(baseEvaluationRecord.getPersonName());
            }
            evaluationRecordVo.setBusinessType(baseEvaluationRecord.getBusinessType());
            evaluationRecordVo.setDoctorName(baseEvaluationRecord.getObjectName());
            evaluationRecordVo.setEvaluationType(baseEvaluationRecord.getEvaluationType());
            evaluationRecordVo.setAddTime(baseEvaluationRecord.getAddTime());
            evaluationRecordVo.setVerifyState(baseEvaluationRecord.getVerifyState());
            evaluationRecordVo.setVerifyDesc(baseEvaluationRecord.getVerifyDesc());
            evaluationRecordVo.setVerifyTime(baseEvaluationRecord.getVerifyTime());
            evaluationRecordVo.setContent(baseEvaluationRecord.getContent());
            evaluationRecordVo.setPhoneNo(baseEvaluationRecord.getPhoneNo());

            if (CommonUtils.isNotEmpty(baseEvaluationRecord.getVerifyUser())) {
                UserVo verifyUser = baseUserManageUnitService.getUserById(baseEvaluationRecord.getVerifyUser());
                if (null != verifyUser) {
                    evaluationRecordVo.setVerifyUser(verifyUser.getUserName());
                }
            }

            //返回问诊类型名称
            if(CommonUtils.isNotEmpty(baseEvaluationRecord.getEvaluationType())){
                String evaluationTypeName = ConsultUtil.getDictionaryValue(DictionaryEnum.CONSULT_TYPE.getDicCode(), baseEvaluationRecord.getEvaluationType());
                evaluationRecordVo.setEvaluationTypeName(evaluationTypeName);
            }

            EvaluationRecordDetailVo queryEvaluationRecordDetail = baseEvaluationRecordService.queryEvaluationRecordDetail(recordId);
            //item
            if(queryEvaluationRecordDetail!=null&&CommonUtils.isNotEmpty(queryEvaluationRecordDetail.getItemList())){
                evaluationRecordVo.setItemList(queryEvaluationRecordDetail.getItemList());
            }
            List<BaseEvaluationLabelVo> evaluationLabelVos =new ArrayList<>();
            if(queryEvaluationRecordDetail!=null&&CommonUtils.isNotEmpty(queryEvaluationRecordDetail.getLabelList())){
                for(EvaluationLabelBean item:queryEvaluationRecordDetail.getLabelList()){
                    BaseEvaluationLabelVo itemVo=new BaseEvaluationLabelVo();
                    BeanUtils.copyProperties(item,itemVo);
                    evaluationLabelVos.add(itemVo);
                }
            }
            evaluationRecordVo.setLabelList(evaluationLabelVos);
        }catch (Exception e){
            LOGGER.error(e.getMessage());
        }
        return evaluationRecordVo;
    }

    /**
     * 居民端-获取医生评价标签
     *
     * @param doctorId
     * @return List<BaseEvaluationLabelrecord>
     * @author yangy
     */
    @RpcService
    @Override
    public List<BaseEvaluationLabelrecordVo> queryDoctorEvaluateLabelList(String doctorId) {
        try {
            return baseEvaluationRecordService.queryDoctorEvaluateLabelList(doctorId);
        } catch (Exception e) {
            LOGGER.info("获取医生评价标签失败");
            return null;
        }
    }

    /**
     * 居民端-获取问诊居民对医生的评价列表
     *
     * @param qo
     * @return List<BaseEvaluationLabelrecord>
     * @author yangy
     */
    @RpcService
    public List<PatientEvaluateDetailVo> queryPatientEvaluateList(PatientEvaluateDetailQo qo) {
        try {
            return baseEvaluationRecordService.queryPatientEvaluateList(qo);
        } catch (Exception e) {
            LOGGER.info("获取医生评价列表失败");
            return null;
        }
    }


    /**
     * 居民端在线问诊普通号详情获取评价标签以及评价信息
     *
     * @param consultDetailRequest
     * @return List<BaseEvaluationLabelrecord>
     */
    @RpcService
    public ConsultDeptEvaluationVo queryEvaluateList(ConsultDetailRequest consultDetailRequest)  throws ControllerException{
        ConsultDeptEvaluationVo consultDeptEvaluationVo = new ConsultDeptEvaluationVo();
        //1.获取机构普通号问诊记录对应的评价记录id
        List<String> evaluateIdList = consultAskServiceIntf.findEvaluateIdByDeptId(consultDetailRequest.getOrgId(), consultDetailRequest.getDeptId());
        if(CommonUtils.isEmpty(evaluateIdList)){
            return consultDeptEvaluationVo;
        }
        List<BaseEvaluationLabelrecordVo> baseEvaluationLabelrecordVoList = Lists.newArrayList();
        try {
            baseEvaluationLabelrecordVoList = baseEvaluationRecordService.queryEvaluateLabelByRecordId(evaluateIdList);
        } catch (Exception e) {
            LOGGER.info("获取医生评价标签失败");
        }
        List<PatientEvaluateDetailVo> patientEvaluateDetailVoList = Lists.newArrayList();
        try {
            PatientEvaluateDetailQo qo = new PatientEvaluateDetailQo();
            qo.setRecordIdList(evaluateIdList);
            patientEvaluateDetailVoList = baseEvaluationRecordService.queryPatientEvaluateList(qo);
        } catch (Exception e) {
            LOGGER.info("获取医生评价列表失败");
        }
        consultDeptEvaluationVo.setBaseEvaluationLabelrecordVoList(baseEvaluationLabelrecordVoList);
        consultDeptEvaluationVo.setPatientEvaluateDetailVoList(patientEvaluateDetailVoList);
        return consultDeptEvaluationVo;
    }
}